<?php
$loader->requireOnce('includes/CorePayment/BillImporter.abstract.php');
$loader->requireOnce('includes/CorePayment/MedicineFeeItem.class.php');
$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('includes/Connectors/DrugConnector.class.php');

class OutMedicineImporter extends BillImporter {
    private $itemList       =   array();
    private $priceList      =   array();
    private $insuranceType   =   array();
	public function readData() {
	    $ticketId     =   $this->storage->getTicketId();
	    $serviceType  =   EnumerationConstant::PAYMENT_ITEM_TYPE_MEDICINE_KEY;
        $normalDrug   =   EnumerationConstant::INSURANCE_GROUP_DRUG_MATERIAL_KEY;
        $cancerDrug   =   EnumerationConstant::INSURANCE_GROUP_CANCER_DRUG_KEY;
                
	    // Get pay_payment_item records of 'service' type
	    $sql    =   "SELECT ppi.pay_payment_item_id, ppi.service_id, ppi.service_name, ppi.unit, ppi.quantity,
	                   ppi.unit_price, dm.insurance_support, dm.cancer_drug
	               FROM pay_payment_item ppi
	                   INNER JOIN pay_payment pp ON (pp.pay_payment_id  = ppi.pay_payment_id
	                       AND pp.ticket_id='$ticketId' AND ppi.enum_item_type = '$serviceType')
	                   LEFT JOIN drug_material dm ON (ppi.service_id = dm.drug_material_id)";
	    $res    =   $this->db->execute($sql);
	    while ($res && !$res->EOF) {
	        $insuranceType =   '';
	        if ($res->fields['insurance_support'] && !$res->fields['cancer_drug']) {
	            $insuranceType =   EnumerationConstant::INSURANCE_GROUP_DRUG_MATERIAL_KEY;
	        }
	        else if ($res->fields['insurance_support'] && $res->fields['cancer_drug']) {
	            $insuranceType =   EnumerationConstant::INSURANCE_GROUP_CANCER_DRUG_KEY;
	        }
	        $billableItem  =   new MedicineFeeItem($res->fields['service_id'], $res->fields['service_name'], $res->fields['unit'],
	           $res->fields['quantity'], $res->fields['unit_price'], $res->fields['unit_price'], $res->fields['unit_price'],
	           $insuranceType, $res->fields['unit_price']);
            $billableItem->setItemId($res->fields['pay_payment_item_id']);
            $this->storage->addItem(PaymentConstant::ITEM_MEDICINE, $billableItem);
	        $res->moveNext();
	    }
    }

    public function updateBillStorage(){
        
    }
}

?>